package com.example.a13001.shoppingmalltemplate.presenter;

import android.content.Context;
import android.content.Intent;

import com.example.a13001.shoppingmalltemplate.manager.DataManager;
import com.example.a13001.shoppingmalltemplate.modle.Address;
import com.example.a13001.shoppingmalltemplate.modle.CommonResult;
import com.example.a13001.shoppingmalltemplate.modle.Message;
import com.example.a13001.shoppingmalltemplate.modle.Notice;
import com.example.a13001.shoppingmalltemplate.mvpview.AdressManagerView;
import com.example.a13001.shoppingmalltemplate.mvpview.MessageView;
import com.example.a13001.shoppingmalltemplate.mvpview.View;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class MessagePredenter implements Presenter{
    private DataManager manager;
    private CompositeSubscription mCompositeSubscription;
    private Context mContext;
    private MessageView mMessageView;
    private Message mMessage;
    private Notice mNotice;
    private CommonResult mCommonResult;



    public MessagePredenter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onCreate() {
        mCompositeSubscription=new CompositeSubscription();
        manager=new DataManager(mContext);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {
        if (mCompositeSubscription.hasSubscriptions()){
            mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void attachView(View view) {
        mMessageView=(MessageView) view;
    }

    @Override
    public void attachIncomingIntent(Intent intent) {

    }

    /**
     * ★ 获取会员消息列表
     * @param companyid
     * @param code
     * @param timestamp
     * @param pagesize
     * @param pageindex
     * @return
     */
    public void getSmsList(String companyid,String code,String timestamp,String status,int pagesize,int pageindex) {

        mCompositeSubscription.add(manager.getSmsList(companyid,code,timestamp,status,pagesize,pageindex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Message>() {
                    @Override
                    public void onCompleted() {
                        if (mMessageView!=null){
                            mMessageView.onSuccess(mMessage);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mMessageView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(Message message) {
                        mMessage=message;
                    }
                }));
    }
    /**
     * ★ 删除会员消息
     * @param companyid
     * @param code
     * @param timestamp
     * @param smsid   会员消息ID
     * @return
     */
    public void doSmsDelete(String companyid,String code,String timestamp,int smsid) {

        mCompositeSubscription.add(manager.doSmsDelete(companyid,code,timestamp,smsid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {
                        if (mMessageView!=null){
                            mMessageView.onSuccessDoSmsDelete(mCommonResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mMessageView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        mCommonResult=commonResult;
                    }
                }));
    }
    /**
     * 获取会员通知列表
     * @param companyid
     * @param code
     * @param timestamp
     * @param pagesize
     * @param pageindex
     * @return
     */
    public void getNoticeList(String companyid,String code,String timestamp,int pagesize,int pageindex) {

        mCompositeSubscription.add(manager.getNoticeList(companyid,code,timestamp,pagesize,pageindex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Notice>() {
                    @Override
                    public void onCompleted() {
                        if (mMessageView!=null){
                            mMessageView.onSuccessNotice(mNotice);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mMessageView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(Notice notice) {
                        mNotice=notice;
                    }
                }));
    }
}
